package com.project.dao.impl;

import com.project.bean.CutPagBean;
import com.project.bean.OrderBean;
import com.project.dao.IOrderDao;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
@Repository
public class OrderDaoImpl extends BaseDao implements IOrderDao {
    @Override
    public CutPagBean<OrderBean> cutAll(int pageNO) {

        CutPagBean<OrderBean> cutBean = new CutPagBean<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from o_order limit ?,?");
            ps.setObject(1, (pageNO - 1) * CutPagBean.PAGESIZE);
            ps.setObject(2, CutPagBean.PAGESIZE);
            rs = ps.executeQuery();
            while (rs.next()) {
                OrderBean list = this.getList(rs);
                cutBean.getList().add(list);
            }

            ps = con.prepareStatement("select count(*) c from o_order");
            rs = ps.executeQuery();
            while (rs.next()) {
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeConnection();
        }
        return cutBean;
    }

    private OrderBean getList(ResultSet rs) {
        OrderBean orderBean = new OrderBean();
        try {
            orderBean.setId(rs.getInt("pk_id"));
            orderBean.setUserName(rs.getString("o_name"));
            orderBean.setOrderDate(LocalDate.parse(rs.getString("o_date")));
            orderBean.setAddress(rs.getString("o_address"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return orderBean;
    }


    @Override
    public void add(OrderBean order) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO o_order(o_name,o_date,o_address)\n" +
                    "VALUES(?,?,?)");
            ps.setObject(1, order.getUserName());
            ps.setObject(2,order.getOrderDate());
            ps.setObject(3, order.getAddress());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps=con.prepareStatement("DELETE FROM o_order WHERE pk_id = ?;");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void update(int id, String address) {
        this.setConnection();
        try {
            ps=con.prepareStatement("UPDATE o_order SET o_address=? WHERE pk_id= ?;");
            ps.setObject(1,address);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public OrderBean findById(int id) {
        OrderBean orderBean = new OrderBean();
        this.setConnection();
        try {
            ps=con.prepareStatement("  SELECT *FROM o_order WHERE pk_id=?;");
            ps.setObject(1,id);
            rs=ps.executeQuery();
            while (rs.next()){
                 orderBean = this.getList(rs);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return orderBean;
    }
}
